//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [],
    // 
    startX: 0, //开始坐标
    startY: 0,
    // 
    addFlag: true,
    typeName: "",
    // 
    uid: "",

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
    })
    this.AgentLoadGoodsStoreType();
  },

  //手指触摸动作开始 记录起点X坐标
  touchstart: function(e) {
    //开始触摸时 重置所有删除
    this.data.items.forEach(function(v, i) {
      //只操作为true的
      if (v.isTouchMove)
        v.isTouchMove = false;
    })
    this.setData({
      startX: e.changedTouches[0].clientX,
      startY: e.changedTouches[0].clientY,
      items: this.data.items
    })
  },
  //滑动事件处理
  touchmove: function(e) {
    var that = this,
      //当前索引
      index = e.currentTarget.dataset.index,
      //开始坐标
      startX = that.data.startX,
      startY = that.data.startY,
      //滑动变化坐标
      touchMoveX = e.changedTouches[0].clientX,
      touchMoveY = e.changedTouches[0].clientY,
      //获取滑动角度
      angle = that.angle({
        X: startX,
        Y: startY
      }, {
        X: touchMoveX,
        Y: touchMoveY
      });
    that.data.items.forEach(function(v, i) {
      v.isTouchMove = false
      //滑动超过30度角 return
      if (Math.abs(angle) > 30) return;
      if (i == index) {
        if (touchMoveX > startX)
          //右滑
          v.isTouchMove = false
        else //左滑
          v.isTouchMove = true
      }
    })
    //更新数据
    that.setData({
      items: that.data.items
    })
  },

  /**
  
  * 计算滑动角度
  
  * @param {Object} start 起点坐标
  
  * @param {Object} end 终点坐标
  
  */
  angle: function(start, end) {
    var _X = end.X - start.X,
      _Y = end.Y - start.Y
    //返回角度 /Math.atan()返回数字的反正切值
    return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
  },
  // 
  AgentLoadGoodsStoreType: function(e) {
    var that = this;
    var uid = this.data.uid;
    var items = this.data.items;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=AgentLoadGoodsStoreType&UID=" + uid),
      success: function(res) {
        console.log(JSON.parse(baseJs.decryUrl(res.data)));
        var json = JSON.parse(baseJs.decryUrl(res.data));
        if (json.ResultFlag == 1) {
          for (var i = 0; i < json.ResultData.length; i++) {
            if ((json.ResultData[i].GoodsPrice.split(".")[0]).length >= 5) {
              json.ResultData[i].GoodsPrice = parseFloat(json.ResultData[i].GoodsPrice / 10000).toFixed(2) + "万";
            } else {
              json.ResultData[i].GoodsPrice = json.ResultData[i].GoodsPrice
            }
          }
          that.setData({
            items: json.ResultData,
          })
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },
  //删除事件
  del: function(e) {
    var that = this;
    var uid = this.data.uid;
    var storeType = e.currentTarget.dataset.typename;
    var items = this.data.items;
    wx.showModal({
      title: '提示',
      content: '是否删除该类型？',
      cancelText: "取消",
      confirmText: "确定",
      success: function(res) {
        if (res.cancel) {

        } else {
          wx.request({
            url: baseJs.api + baseJs.encryUrl("method=AgentDeleteGoodsStoreType&UID=" + uid + "&StoreType=" + storeType),
            success: function(res) {
              // console.log(JSON.parse(baseJs.decryUrl(res.data)));
              var json = JSON.parse(baseJs.decryUrl(res.data));
              if (json.ResultFlag == 1) {
                wx.showToast({
                  title: json.ResultMsg,
                  icon: 'none',
                  duration: 2000
                });
                items.splice(e.currentTarget.dataset.index, 1);
                that.setData({
                  items: items
                })
              } else {
                wx.showToast({
                  title: json.ResultMsg,
                  icon: 'none',
                  duration: 2000
                });
              }
            }
          })
        }
      },
    })
  },
  // 
  addCancel: function() {
    this.setData({
      addFlag: true,
    })
  },
  // 
  addType: function() {
    this.setData({
      addFlag: false,
    })
  },
  // 
  typeName: function(e) {
    this.setData({
      typeName: e.detail.value
    })
  },
  // 
  addConfirm: function() {
    var that = this;
    var uid = this.data.uid;
    var typeName = this.data.typeName;
    this.setData({
      addFlag: true
    })
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=AgentAddGoodsStoreType&UID=" + uid + "&StoreType=" + typeName),
      success: function(res) {
        // console.log(JSON.parse(baseJs.decryUrl(res.data)));
        var json = JSON.parse(baseJs.decryUrl(res.data));
        if (json.ResultFlag == 1) {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
          that.AgentLoadGoodsStoreType();
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },
  //  
  typeTap: function(e) {
    wx.navigateTo({
      url: '/packageA/yunYGoodsList/yunYGoodsList?typeName=' + e.currentTarget.dataset.typename,
    })
  },
  // 
})